#include <stdio.h>
#include <unistd.h>
#include "veml7700.h"


int main(int argc,int *argv[])
{
    printf("hello world\n");
    int fd = 0;

    fd = wiringPiI2CSetup(I2C_ADDRESS);
    uint16_t register_cache[4];

    // write initial state to VEML7700
  register_cache[0] = ( ((uint16_t)(ALS_GAIN_x2) << ALS_SM_SHIFT) |
                        ((uint16_t)(ALS_INTEGRATION_100ms) << ALS_IT_SHIFT) |
                        ((uint16_t)(ALS_PERSISTENCE_1) << ALS_PERS_SHIFT) |
                        ((uint16_t)(0) << ALS_INT_EN_SHIFT) |
                        ((uint16_t)(0) << ALS_SD_SHIFT) );
  register_cache[1] = 0x0000;
  register_cache[2] = 0xffff;
  register_cache[3] = ( ((uint16_t)(ALS_POWER_MODE_3) << PSM_SHIFT) |
                        ((uint16_t)(0) << PSM_EN_SHIFT) );
  for (uint8_t i=0; i<4; i++){
    sendData(fd,i, register_cache[i]);
  }

  // wait at least 2.5ms as per datasheet
  delay(3);

  float lux;

  while (1)
  {

    setIntegrationTime(fd,ALS_INTEGRATION_100ms);



    setPower(fd,0);
    setGain(fd,ALS_GAIN_d8);
    setPower(fd,1);
    sampleDelay(fd);
    getALSLux(fd,&lux);
    printf("get lux %f\n",lux);

    setPower(fd,0);
    setGain(fd,ALS_GAIN_d4);
    setPower(fd,1);
    sampleDelay(fd);
    getALSLux(fd,&lux);
    printf("get lux %f\n",lux);

    setPower(fd,0);
    setGain(fd,ALS_GAIN_x1);
    setPower(fd,1);
    sampleDelay(fd);
    getALSLux(fd,&lux);
    printf("get lux %f\n",lux);

    setPower(fd,0);
    setGain(fd,ALS_GAIN_x2);
    setPower(fd,1);
    sampleDelay(fd);
    getALSLux(fd,&lux);
    printf("get lux %f\n",lux);        


#if 0
    setGain(fd,ALS_GAIN_d8);

    setPower(fd,0);
    setIntegrationTime(fd,ALS_INTEGRATION_25ms);
    setPower(fd,1);
    sampleDelay(fd);
    getALSLux(fd,&lux);
    printf("get lux %f\n",lux);

    setPower(fd,0);
    setIntegrationTime(fd,ALS_INTEGRATION_50ms);
    setPower(fd,1);
    sampleDelay(fd);
    getALSLux(fd,&lux);
    printf("get lux %f\n",lux);

    setPower(fd,0);
    setIntegrationTime(fd,ALS_INTEGRATION_100ms);
    setPower(fd,1);
    sampleDelay(fd);
    getALSLux(fd,&lux);
    printf("get lux %f\n",lux); 

    setPower(fd,0);
    setIntegrationTime(fd,ALS_INTEGRATION_400ms);
    setPower(fd,1);
    sampleDelay(fd);
    getALSLux(fd,&lux);
    printf("get lux %f\n",lux); 

    setPower(fd,0);
    setIntegrationTime(fd,ALS_INTEGRATION_800ms);
    setPower(fd,1);
    sampleDelay(fd);
    getALSLux(fd,&lux);
    printf("get lux %f\n",lux); 

#endif
    sleep(1);
  }




    return 0;
}